package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.mapper.entity.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserManageMapper extends BaseMapper<SysUser> {
    @Select("SELECT * FROM sys_user_view WHERE role_id != 4")
    List<SysUserView> getUserList();

    @Delete("DELETE FROM `sys_user` WHERE user_id = #{userId}")
    int deleteSysUser(int userId);
    @Delete("DELETE FROM `sys_user_role` WHERE user_id = #{userId}")
    int deleteSysRole(int userId);

    @Delete("<script>" +
            "DELETE FROM sys_user" +
            " WHERE user_id IN" +
            " <foreach collection='list' index='index' open='(' separator=',' close=')' item='userList' >" +
            "   #{userList.userId}" +
            " </foreach>" +
            "</script>")
    void deleteUserList(List<SysUserView> userList);

    @Delete("<script>" +
            "DELETE FROM sys_user_role" +
            " WHERE user_id IN" +
            " <foreach collection='list' index='index' open='(' separator=',' close=')' item='userList' >" +
            "   #{userList.userId}" +
            " </foreach>" +
            "</script>")
    void deleteRoleList(List<SysUserView> userList);

    @Select("SELECT user_name FROM sys_user WHERE user_id = #{userId}")
    String selectCreaterName(int stkId);

    @Select("SELECT school_id FROM sys_user WHERE user_id = #{userId}")
    String selectSchoolName(int stkId);

    @Select("SELECT count(1) FROM sys_user WHERE number = #{userNumber}")
    int countByUserNum(String userNumber);

    @Select("SELECT count(1) FROM sys_user WHERE user_id = #{userId}")
    int countByUserId(int userId);

    @Insert("INSERT INTO sys_user_role(user_id, role_id)" +
            "   VALUES( #{userId}, #{roleId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertUserRole(SysUserRole newUserRole);

    @Select("SELECT user_id FROM sys_user WHERE user_number = #{number}")
    int selectId(String number);

    @Insert("INSERT INTO sys_user(number,name,avatar,phone,email,password, school_id, state) " +
            "   VALUES(#{number},#{name}, #{avatar},#{phone},#{email},#{password}, #{schoolId}, #{state})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    void insertUser(SysUser newUser);

    @Select("SELECT * FROM sys_user_role WHERE user_id = #{userId}")
    SysRole getRoleByUserId(int userId);

    @Update("UPDATE `sys_user_role` SET role_id = #{roleId} WHERE user_id = #{userId}")
    void updateUserRoleId(SysUserView user);

    @Update("UPDATE `sys_user` SET number = #{number}, name = #{name} WHERE user_id = #{userId}")
    void updateUserById(SysUser curUser);
    @Select("SELECT * FROM advertiser_view")
    List<AdvertiserView> getAdvertiserList();
    @Select("SELECT * FROM advertiser_view WHERE user_id = #{userId}")
    List<AdvertiserView> getAdvertiserDataList(int userId);

    @Insert("INSERT INTO advertiser(user_id,company_name,contact_name,website,store_address) " +
            "   VALUES(#{userId},#{companyName}, #{contactName},#{website},#{storeAddress})")
    @Options(useGeneratedKeys = true, keyProperty = "advertiserId", keyColumn = "advertiser_id")
    int insertAdvertiser(AdvertiserView request);

    @Delete("DELETE FROM `advertiser` WHERE user_id = #{userId}")
    int deleteAdvertiserList(AdvertiserView request);

    @Delete("<script>" +
            "DELETE FROM sys_user_role" +
            " WHERE user_id IN" +
            " <foreach collection='list' index='index' open='(' separator=',' close=')' item='advertiserList' >" +
            "   #{advertiserList.userId}" +
            " </foreach>" +
            "</script>")
    int batchDeleteSysRole(List<AdvertiserView> advertiserList);

    @Delete("<script>" +
            "DELETE FROM advertiser" +
            " WHERE user_id IN" +
            " <foreach collection='list' index='index' open='(' separator=',' close=')' item='advertiserList' >" +
            "   #{advertiserList.userId}" +
            " </foreach>" +
            "</script>")
    int batchDeleteAdvertiserList(List<AdvertiserView> advertiserList);

    @Delete("<script>" +
            "DELETE FROM sys_user" +
            " WHERE user_id IN" +
            " <foreach collection='list' index='index' open='(' separator=',' close=')' item='advertiserList' >" +
            "   #{advertiserList.userId}" +
            " </foreach>" +
            "</script>")
    int batchDeleteSysUser(List<AdvertiserView> advertiserList);

    @Update("UPDATE `advertiser` " +
            "SET company_name = #{companyName},contact_name=#{contactName}," +
            "website = #{website},store_address = #{storeAddress} " +
            "WHERE user_id = #{userId}")
    int updateAdvertiser(AdvertiserView request);

    @Update("UPDATE `sys_user` " +
            "SET number = #{number},name = #{name},phone = #{phone},email = #{email}" +
            " WHERE user_id = #{userId}")
    int updateAdvertiserUserById(AdvertiserView request);

    /*  @Insert("INSERT INTO sys_user(number,name,avatar,phone,email,password, school_id, state) " +
            "   VALUES(#{number},#{name}, #{avatar},#{phone},#{email},#{password}, #{schoolId}, #{state})")
  */
}
